package com.sip.stream.utils.zfy.voice;

import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;

import com.blankj.utilcode.util.SPUtils;
import com.sip.stream.ClientApp;
import com.sip.stream.utils.zfy.status.ZFYStatus;

import java.util.Locale;

public class BeepTool {
    private static final String TAG = "BeepTool";
    private static String cacheString;
    private static BeepTool instance;
    private ZFYBeepConfig ZFYBeepConfig;
    private AudioManager audioManager;
    private AbstractBeepProvider beepProvider;
    private Vibrator vibrator;
    private String typeName = BeepType.SPEAK;
    private long lastBatteryTime = 0;

    public static BeepTool getInstance() {
        if (instance == null) {
            synchronized (BeepTool.class) {
                if (instance == null) {
                    instance = new BeepTool();
                }
            }
        }
        return instance;
    }

    public void setTypeName(String typeName) {
        AbstractBeepProvider abstractBeepProvider;
        if (typeName != null) {
            String name = this.typeName;
            if (name != null && !name.equalsIgnoreCase(typeName) && (abstractBeepProvider = this.beepProvider) != null) {
                abstractBeepProvider.release();
                this.beepProvider = null;
            }
            this.typeName = typeName;
            getBeepProvider();
        }
    }

    public void reBuild() {
        Log.i(TAG, "reBuild");
        AbstractBeepProvider abstractBeepProvider = this.beepProvider;
        if (abstractBeepProvider != null) {
            abstractBeepProvider.release();
            this.beepProvider = null;
        }
        getBeepProvider();
    }

    public void restart() {
        Log.i(TAG, "restart");
    }

    private AbstractBeepProvider getBeepProvider() {
        if (this.beepProvider == null) {
            if (this.typeName.equalsIgnoreCase(BeepType.SPEAK)) {
                TTSProvider tTSProvider = new TTSProvider();
                this.beepProvider = tTSProvider;
                tTSProvider.setUp(new GSCallBack<Boolean>() {
                    @Override
                    public void onComplete(GSResult<Boolean> gSResult) {
                        if (gSResult.isSuccess()) {
                            return;
                        }
                        Log.i(TAG, "tts init failed,use raw");
                        BeepTool.this.changeToRaw();
                    }
                });
            } else if (this.typeName.equalsIgnoreCase(BeepType.VIBRATOR)) {
                this.beepProvider = new VibratorBeepPovider();
            } else {
                this.beepProvider = new BeepProvider();
            }
        }
        return this.beepProvider;
    }

    public void changeToRaw() {
        this.beepProvider = new RawProvider();
    }

    private BeepTool() {
    }

    public static void play(String action, String message) {
        getInstance().autoPauseAlarm();
        getInstance().playInstance(action, message);
    }

    public static void play(String action) {
        getInstance().autoPauseAlarm();
        getInstance().playInstance(action, null);
    }

    public static void playString(String message) {
        play(BeepType.START, message);
    }

    public static void playString(int type, String message) {
        if (type == 1) {
            cacheString = message;
        } else if (type == 2) {
            cacheString(BeepType.VIDEO_START, message);
        } else {
            playString(message);
        }
    }

    public static void cacheString(String key, String message) {
        SPUtils.getInstance().put(key,message);
    }

    public static void playCacheString() {
        String message = cacheString;
        if (message == null) {
            return;
        }
        playString(message);
        cacheString = null;
    }

    public static void playTime(long time) {
        getInstance().getBeepProvider().playTime(time);
    }

    public void changeLanguage(Locale locale) {
        getInstance().getBeepProvider().changeLanguage(locale);
    }

    public void changeAudioAttributes(boolean z) {
        getInstance().getBeepProvider().changeAudioAttributes(z);
    }

    private void playInstance(String action, String message) {
        String tempMsg;
        if (getBeepConfig().getGB28181PromptTone() == 0 || !(action.equalsIgnoreCase(BeepType.VIDEO_CALL_CONNECTED) || action.equalsIgnoreCase(BeepType.VIDEO_CALL_FINISHED) || action.equalsIgnoreCase(BeepType.VOIP_CALL_CONNECTED) || action.equalsIgnoreCase(BeepType.VOIP_CALL_FINISHED))) {
            if (getBeepConfig().getFRPromptTone() != 0) {
                Log.i(TAG, "getFRPromptTone != 0");
                if (action.equalsIgnoreCase(BeepType.FOUND_ABNORMAL_PERSON) || action.equalsIgnoreCase(BeepType.FOUND_ABNORMAL_VEHICLE)) {
                    return;
                }
            }
            if (action.equalsIgnoreCase(BeepType.LOW_BATTERY)) {
                long currentTimeMillis = System.currentTimeMillis();
                if (currentTimeMillis - this.lastBatteryTime < 15000) {
                    return;
                }
                this.lastBatteryTime = currentTimeMillis;
            }
            if (action.equalsIgnoreCase(BeepType.VIDEO_START) && (tempMsg = cacheString) != null) {
                cacheString = null;
                message = tempMsg;
            }
            getBeepProvider().play(action, message);
        }
    }

    public static void beepOneShot() {
        getInstance().instanceBeepOneShot();
    }

    private void instanceBeepOneShot() {
        getInstance().getBeepProvider().beepOneShot();
    }

    private Vibrator getVibrator() {
        if (this.vibrator == null) {
            this.vibrator = (Vibrator) ClientApp.clientApp.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
        }
        return this.vibrator;
    }

    private AudioManager getAudioManager() {
        if (this.audioManager == null) {
            this.audioManager = (AudioManager)ClientApp.clientApp.getSystemService(Context.AUDIO_SERVICE);
        }
        return this.audioManager;
    }

    public static void vibratorOneShot() {
        if (ZFYStatus.getInstance().isSilent() || getInstance().getAudioManager() == null || getInstance().getAudioManager().getRingerMode() == 0) {
            return;
        }
        if (Build.VERSION.SDK_INT >= 26) {
            getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(500L, 1));
        } else {
            getInstance().getVibrator().vibrate(500L);
        }
    }

    public ZFYBeepConfig getBeepConfig() {
        if (this.ZFYBeepConfig == null) {
            this.ZFYBeepConfig = new ZFYBeepConfig();
        }
        return this.ZFYBeepConfig;
    }

    public void setBeepConfig(ZFYBeepConfig zFYBeepConfig) {
        this.ZFYBeepConfig = zFYBeepConfig;
    }

    public void playRedBlueAlarm(boolean open) {

    }

    public void stopRedBlueAlarm() {
        getBeepProvider().playRedBlueAlarm(false);
    }

    private void autoPauseAlarm() {
        getBeepProvider().autoPauseAlarm();
    }

    public static String insertDelimiter(String str, char c) {
        StringBuilder sb = new StringBuilder();
        for (char c2 : str.toCharArray()) {
            sb.append(c2).append(c);
        }
        return sb.deleteCharAt(sb.length() - 1).toString();
    }
}
